"""
主界面QMainWindow
1、菜单栏
2、状态栏
3、工具栏
3、QDockWidget和QCentralWidget
"""
import sys
from typing import Optional
from PySide6.QtGui import QAction, QKeySequence, Qt
from PySide6.QtWidgets import QApplication, QDockWidget, QLabel, QMainWindow, QMenu, QMenuBar, QPlainTextEdit, QStatusBar, QToolBar, QWidget


class MainWindow(QMainWindow):
    """
    主窗口组件，内置的特殊QWidget
    包括QMenuBar, QStatusBar, QDockArea
    """
    def __init__(self, parent: Optional[QWidget] = None) -> None:
        super().__init__(parent=parent)
        self.setWindowFlags(Qt.CustomizeWindowHint | Qt.WindowContextHelpButtonHint | Qt.WindowCloseButtonHint)    # 关闭默认按钮，添加帮助按钮
        self.__init_menubar()
        self.__init_statusbar()
        self.__init_sub_widget()
        self.__init_dock_widget()
        self.__init_toolbar()
    
    def __init_menubar(self):
        menu_bar = QMenuBar()
        file_menu = QMenu("&文件")
        file_menu.addAction(QAction("&新建文件", self, shortcut=QKeySequence.New,
                                    statusTip="创建新的文件", triggered=self.__slot_new_file))
        file_menu.addSeparator()
        file_menu.addAction(QAction("&退出", self, shortcut=QKeySequence.Close, triggered=self.close))
        menu_bar.addMenu(file_menu)
        self.setMenuBar(menu_bar)
    
    def __init_statusbar(self):
        status_bar = QStatusBar()
        label1 = QLabel("区域1")
        label2 = QLabel("区域2")
        labelp = QLabel("永久区域") # 永久区域在右下角，不会被showMessage覆盖
        status_bar.addWidget(label1)
        status_bar.addWidget(label2)
        status_bar.addPermanentWidget(labelp)
        status_bar.showMessage("临时显示3秒钟", 3000)   # 定时显示多少
        self.setStatusBar(status_bar)

    def __init_toolbar(self):
        toolbar = QToolBar()
        self.addToolBar(toolbar)
        action = self.dock_right.toggleViewAction()
        action.setWhatsThis("打开或收起右侧的dock")
        toolbar.addAction(action)
        toolbar.addAction(self.dock_left.toggleViewAction())
        toolbar.addSeparator()
        toolbar.addAction(self.dock_free.toggleViewAction())

    def __init_sub_widget(self):
        self.left_editor = QPlainTextEdit()
        self.left_editor.setPlainText("左侧的编辑器")
        self.right_editor = QPlainTextEdit()
        self.right_editor.setPlainText("右侧的编辑器")
        self.free_editor = QPlainTextEdit()
        self.free_editor.setPlainText("自由的编辑器")
        central_editor = QPlainTextEdit()
        central_editor.setPlainText("中间的编辑器")
        self.setCentralWidget(central_editor)   # 中心区域组件周围包围dock组件

    def __init_dock_widget(self):
        self.dock_left = QDockWidget("左侧docker")
        self.dock_left.setWidget(self.left_editor)
        self.dock_left.setAllowedAreas(Qt.LeftDockWidgetArea)    # 限制允许dock的区域
        self.dock_right = QDockWidget("右侧docker")
        self.dock_right.setWidget(self.right_editor)             # 限制允许dock的区域
        self.dock_right.setAllowedAreas(Qt.RightDockWidgetArea)
        self.dock_free = QDockWidget("自由docker")
        self.dock_free.setWidget(self.free_editor)
        self.addDockWidget(Qt.RightDockWidgetArea, self.dock_left)   # 初始区域可以与限制区域不同，但一旦拖走就不能再设置
        self.addDockWidget(Qt.RightDockWidgetArea, self.dock_right)  
        self.addDockWidget(Qt.TopDockWidgetArea, self.dock_free)  
    
    def __slot_new_file(self):
        print("已经创建新的文件")

if __name__ == "__main__":
    app = QApplication(sys.argv)        # 主流程控制，一个程序只能有一个QApplication，sys可以用于命令行导入
    window = MainWindow()
    window.show()
    sys.exit(app.exec())               # 控制程序安全退出
